// 函数字面量 四部分组成
var add = function(a, b) {
    return a + b;
};

// 调用的四种模式
// 1.方法调用模式
var myObject = {
    value: 0;
    increment(inc) {
        this.value += typeof inc === 'number' ? inc : 1
    }
};
myObject.increment();
document.writeln(myObject.value);

myObject.increment(2);
document.writeln(myObject.value);

// 2.函数调用模式
myObject.double = function() {
    var that = this;
    var helper = function() {
        that.value = add(that.value, that.value);
    };
    helper();
};
myObject.double();
document.writeln(myObject.getValue);

//构造器调用模式
var Quo = function(string) {
    this.status = string;
};
Quo.prototype.get_status = function() {
    return this.status;
};
var myQuo = new Quo("confused");
document.writeln(myQuo.get_status());

//Apply调用模式
var array = [3, 4];
var sum = add.apply(null, array);
var statusObject = {
    status: 'A-OK'
};
var status = Quo.prototype.get_status.apply(statusObject); //A-OK

//参数
var sum = function() {
    var i, sum = 0;
    for (i = 0; i < arguments.length; i++) {
        sum += arguments[i];
    }
    return sum;
};
document.writeln(sum(4, 6, 77, 7));

//异常
var add = function(a, b) {
    if (typeof a !== 'number' || typeof b !== 'number') {
        throw {
            name: 'typeerror',
            message: 'add needs number'
        };
    }
    return a + b;
}
var try_it = function() {
    try {
        add("seven");
    } catch (e) {
        document.writeln(e.name + ':' + e.message);
    }
}
try_it();

//给类型添加方法
Function.prototype.method = function(name, func) {
    this.prototype[name] = func;
    return this;
};
Number.method('integer', function() {
    return Math[this < 0 ? 'ceiling' : 'floor'](this);
})
document.writeln((-10 / 3).integer());

//递归
var hanoi = function(disc, src, aux, dst) {
    if (disc > 0) {
        hanoi(disc - 1, src, dst, aux);
        document.writeln('move disc' + disc + 'from' + src + 'to' + dis);
        hanoi(disc - 1, aux, src, dst);
    }
}
hanoi(3, 'src', 'aux', 'dst');

//作用域
var foo = function() {
    var a = 3,
        b = 5;
    var bar = function() {
        var b = 7,
            c = 11;
        a += b + c;
    };
    bar();
}

//闭包
var myobject = function() {
    var value = 0;
    return {
        increment(inc) {
            value += typeof inc === 'number' ? inc : 1;
        },
        getValue() {
            return value;
        }
    }
}();

//定义一个函数，设置一个节点为黄色，然后逐渐变为白色
var fade = function(node) {
    var level = 1;
    var step = function() {
        var hex = level.toString(16);
        node.style.backgroundColor = '#FFF' + hex + hex;
        if (level < 15) {
            level += 1;
            setTimeout(step, 100);
        }
    };
    setTimeout(step, 100);
};
fade(document.body);

//回调
request = prepare_the_request();
send_request_asynchronously(request, function(request) {
    display(response);
});

//模块
var serial_maker = function() {
    var prefix = '';
    var seq = 0;
    return {
        set_prefix: function(p) {
            prefix = String(p);
        },
        set_seq: function(s) {
            seq = s;
        },
        gensym: function() {
            var result = prefix + seq;
            seq += 1;
            return result;
        }
    };
};
var seqer = serial_maker();
seqer.set_prefix('Q');
seqer.set_seq(1000);
var unique = seqer.gensym();

// 套用
Function.method('curry', function() {
    var slice = Array.prototype.slice,
        args = slice.apply(arguments),
        that = this;
    return function() {
        return that.apply(null, args.concat(slice.apply(arguments)));
    };
});